using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Source;

namespace OR.Lazarus.Library.DataTier.Source.Lazarus
{
	/// <summary>
	/// Defines an intersection entity between two or more entities.
	/// </summary>
   public class IntersectionDetail : EntityBase
   {
		protected System.String mName;
		protected string mAlias;
		protected string mFriendlyName;
		protected string mLogicalName;
		protected string mFilegroup;
		protected string mDescription;
	
		protected Derived.ReferenceCollection mReferences;
		protected Derived.FieldCollection mFields;
		protected Derived.MethodCollection mMethods;

		public IntersectionDetail()
		{
			mReferences = new Derived.ReferenceCollection();
			mReferences.ParentEntity = this;
			mFields = new Derived.FieldCollection();
			mFields.ParentEntity = this;
			mMethods = new Derived.MethodCollection();
			mMethods.ParentEntity = this;
	
		}
	
		/// <summary>
		/// Specifies a unique name for the entity.
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Alias
		{
			get
			{
				return ( mAlias );
			}
			set
			{
				mAlias = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string FriendlyName
		{
			get
			{
				return ( mFriendlyName );
			}
			set
			{
				mFriendlyName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string LogicalName
		{
			get
			{
				return ( mLogicalName );
			}
			set
			{
				mLogicalName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Filegroup
		{
			get
			{
				return ( mFilegroup );
			}
			set
			{
				mFilegroup = value;
			}
		}

		/// <summary>
		/// Provides a long-text description for the entity.
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.ReferenceCollection References
		{
			get
			{
				return ( mReferences );
			}
			set
			{
				mReferences = value;
			}
		}

		public Derived.FieldCollection Fields
		{
			get
			{
				return ( mFields );
			}
			set
			{
				mFields = value;
			}
		}

		public Derived.MethodCollection Methods
		{
			get
			{
				return ( mMethods );
			}
			set
			{
				mMethods = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mAlias = DataUtility.GetString( pSource, "alias" );
			mFriendlyName = DataUtility.GetString( pSource, "friendly-name" );
			mLogicalName = DataUtility.GetString( pSource, "logical-name" );
			mFilegroup = DataUtility.GetString( pSource, "filegroup" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lz:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vReferences = ( XmlElement )pSource.SelectSingleNode( "lz:references", PersistenceConfig.Namespaces );
			
			if ( vReferences != null )
			{
				mReferences.LoadXml( vReferences );
			}
			XmlElement vFields = ( XmlElement )pSource.SelectSingleNode( "lz:fields", PersistenceConfig.Namespaces );
			
			if ( vFields != null )
			{
				mFields.LoadXml( vFields );
			}
			XmlElement vMethods = ( XmlElement )pSource.SelectSingleNode( "lz:methods", PersistenceConfig.Namespaces );
			
			if ( vMethods != null )
			{
				mMethods.LoadXml( vMethods );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lz", "intersection", "http://schemas.icitadel.com/Lazarus/Source.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mAlias ) )
			{
				vNewElement.SetAttribute( "alias", mAlias );
			}
					
			if ( !String.IsNullOrEmpty( mFriendlyName ) )
			{
				vNewElement.SetAttribute( "friendly-name", mFriendlyName );
			}
					
			if ( !String.IsNullOrEmpty( mLogicalName ) )
			{
				vNewElement.SetAttribute( "logical-name", mLogicalName );
			}
					
			if ( !String.IsNullOrEmpty( mFilegroup ) )
			{
				vNewElement.SetAttribute( "filegroup", mFilegroup );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lz", "description", "http://schemas.icitadel.com/Lazarus/Source.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mReferences.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mFields.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mMethods.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.IntersectionDetail v_oReturn = new Derived.IntersectionDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Alias = mAlias;
			v_oReturn.FriendlyName = mFriendlyName;
			v_oReturn.LogicalName = mLogicalName;
			v_oReturn.Filegroup = mFilegroup;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.ReferenceDetail vDetail in mReferences )
			{
				v_oReturn.References.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.FieldDetail vDetail in mFields )
			{
				v_oReturn.Fields.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.MethodDetail vDetail in mMethods )
			{
				v_oReturn.Methods.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.IntersectionDetail CloneTyped()
		{
			return ( ( Derived.IntersectionDetail )this.Clone() );
		}
   }
}
   